package org.bbelovic.devel.exercises.permissions.bitfield;

public class BitfieldPermissions {

    public static final long NO_PERMISSIONS = 0L;
    public static final long READ_PERMISSIONS = 2L;
    public static final long WRITE_PERMISSIONS = 4L;
    private long permissions = 0L;

    public void setPermissions(final long permissions) {
        this.permissions |= permissions;
    }

    public long getPermissions() {
        return this.permissions;
    }

    public void removePermission(final long permission) {
        this.permissions = this.permissions & ~permission;
    }

    public boolean isReadEnabled() {
        return isPermissionEnabled(READ_PERMISSIONS);
    }

    public boolean isWriteEnabled() {
        return isPermissionEnabled(WRITE_PERMISSIONS);
    }
    
    private boolean isPermissionEnabled(final long permissionToCheck) {
        return (this.permissions & permissionToCheck) == permissionToCheck;
    }
}
